import { OrganizationConst } from "src/consts/organization_const";
import { AccManageConst } from "src/consts/acc_manage_const";
import { PopupName } from "src/consts/popup_const";
import * as Api from 'src/api/net';

const state: any = {} as any;
// StateIniter.init(state, 'demo');

// state.formData = {};
state.leader_list = []
state.id = ""
state.name = ""
state.remark = ""
state.feature_code = ""
state.mem_num = ""
state.mem_list = []

// vuex

/** 更新数据的 */
const mutations = {
    [OrganizationConst.CommitOrgShowList](state: any, data: any): void {
        console.log("org_Panel data update", data);
        state.id = data.id
        state.name = data.name
        state.remark = data.remark
        state.mem_num = data.mem_num
        state.feature_code = data.feature_code
        state.leader_list = data.leader_list
        state.mem_list = data.mem_list
        if (WLStore().state.org.type == "edit") {
            WLLib.panel(PopupName.Org_Panel);
        } else if (WLStore().state.org.type == "add") {
            WLLib.panel(PopupName.Org_Add_Panel);
        }

    },
    [OrganizationConst.CommitOrgShowListAcc](state: any, data: any): void {
        for (let k = 0; k < data.length; k++) {
            var flag = true
            for (let i = 0; i < state.mem_list.length; i++) {
                if (state.mem_list[i].id == data[k].id) {
                    flag = false
                }
            }
            if (flag) {
                state.mem_list.push(data[k])
            }
        }
        WLLib.panel(PopupName.Org_Acc_Panel).close()
    },
    [OrganizationConst.CommitOrgShowListDelAcc](state: any, data: any): void {
        for (let i = 0; i < state.mem_list.length; i++) {
            if (state.mem_list[i].id == data) {
                state.mem_list.splice(i, 1)
                WLMessage("删除部门成员成功", "success")
            }
        }
    }
}

//异步处理
const actions = {
    [OrganizationConst.DispatchOrgShow](context: VuexData, data?: any): void {
        Api.AjaxPost(Config.ajaxUrl + "/dept/show.do", data, (res: any) => {
            if (isNoEmptyObject(res.data)) {
                let senData: any = {}
                senData["curr_page"] = 1
                senData["page_size"] = 99999
                Api.AjaxPost(Config.ajaxUrl + "/account/list.do", senData, (accRes: any) => {
                    if (isNoEmptyObject(accRes.data)) {
                        var list = res.data.mem_list
                        var acc_list = accRes.data.data_list
                        if (isNoEmpty(list)) {
                            for (let i = 0; i < list.length; i++) {
                                if (isNoEmpty(acc_list)) {
                                    for (let k = 0; k < acc_list.length; k++) {
                                        if (list[i].id == acc_list[k].id) {
                                            res.data.mem_list[i] = acc_list[k]
                                        }
                                    }
                                }
                            }
                        }
                        WLCommit(OrganizationConst.CommitOrgShowList, res.data);
                    }
                })
            }
        })
    },
    [OrganizationConst.DispatchOrgEdit](context: VuexData, data?: any): void {
        var editData: any = {}
        editData.id = data.id
        editData.name = data.name
        editData.remark = data.remark
        editData.leader_list = []
        if (isNoEmpty(data.leader_list)) {
            for (let i = 0; i < data.leader_list.length; i++) {
                var obj: any = {}
                obj.id = data.leader_list[i].id
                editData.leader_list.push(obj)
            }
        }
        Api.AjaxPost(Config.ajaxUrl + "/dept/edit.do", editData, (res: any) => {
            if (isNoEmpty(data.mem_list)) {
                for (let k = 0; data.mem_list.length; k++) {
                    var addData: any = {}
                    addData.user_id = data.mem_list[k].id
                    addData.dept_id = data.id
                    Api.AjaxPost(Config.ajaxUrl + "/dept/mem/add.do", addData, (res: any) => {
                        if (k == data.mem_list.length - 1) {
                            WLMessage("编辑部门成功", "success")
                            let senData: any = {}
                            senData["curr_page"] = WLStore().state.org.curr_page
                            senData["page_size"] = WLStore().state.org.page_size
                            senData.level = 0
                            WLDispatch(OrganizationConst.GetOrgTreeData, senData);
                            WLLib.panel(PopupName.Org_Panel).close()
                        }
                    })
                }
            }
            else {
                WLMessage("编辑部门成功", "success")
                let senData: any = {}
                senData["curr_page"] = WLStore().state.org.curr_page
                senData["page_size"] = WLStore().state.org.page_size
                senData.level = 0
                WLDispatch(OrganizationConst.GetOrgTreeData, senData);
                WLLib.panel(PopupName.Org_Panel).close()
            }
        })
    },
    [OrganizationConst.DispatchOrgShowListDelAcc](context: VuexData, data?: any): void {
        var sendData: any = {}
        sendData.user_id = data
        sendData.dept_id = WLStore().state.orgShow.id
        Api.AjaxPost(Config.ajaxUrl + "/dept/mem/del.do", sendData, (res: any) => {
            WLCommit(OrganizationConst.CommitOrgShowListDelAcc, data)
        })
    }
}

export default {
    state,
    mutations,
    actions
}